<head>
	<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<div>
	<form enctype="multipart/form-data" accept-charset="utf-8" action="QueryByHum.php" id="sf" method="POST">
		<img src="logo.jpg" alt="alt" id="resultlogo"/>
		<div id="resultupp">
			<input name="upfile" type="file"/>
			<input type="submit" value="SEARCH AGAIN" id="submitbutton">
		</div>
		
	</form>
</div>

<?php
	function cmpNotes($cut_arr, $whole_arr) {
		$isFound = false;
		for($i=0;$i<(count($whole_arr)-count($cut_arr)+1);$i++) {
			if ($cut_arr[0] == $whole_arr[$i]) {
				$j = $i;
				foreach ($cut_arr as $note) {
					if ($note != $whole_arr[$j]) {
						$isFound = false;
						break;
					}
					else {
						$isFound = true;
						$j++;
					}
				}
				if ($isFound)
					break;
			}
		}
		return $isFound;
	}

	function cmpChannel($cut_arr, $whole_arr) {
		$isFound = false;
		$lookup = array_keys($cut_arr);
		$lookdown = array_keys($whole_arr);
		for($i=0;$i<count($cut_arr);$i++) {
			for($j=0;$j<count($whole_arr);$j++) {
				if (cmpNotes($cut_arr[$lookup[$i]], $whole_arr[$lookdown[$j]])) {
					$isFound = true;
					break;
				}
			}
			if (!$isFound)
				break;
		}
		return $isFound;
	}
	
	require_once dirname(dirname(__FILE__)) . '/Midi/loader.php';
    use \Midi\Parsing\FileParser;
    use \Midi\Translating\TextFormatter;
    use \Midi\Translating\Printer;
	
	//echo "always print this   " .  $_FILES['upfile']['tmp_name'];
    if (!is_uploaded_file($_FILES['upfile']['tmp_name']))
    	echo "<h2>Input is empty, please search by uploading a file :)</h2>";
    elseif ($_FILES['upfile']['type'] != "audio/mid")
    	echo "<h2>You should upload a midi file!</h2>";
    else {
	$uploaddir = dirname(dirname(__FILE__)) . '/Midi/Cut/';
	$uploadfile = $uploaddir . basename($_FILES['upfile']['name']);
	
    $file = $uploadfile;
    
    if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile)) {
    	//echo "File is valid, and was successfully uploaded.\n";
	} else {
    	//echo "Possible file upload attack!\n";
	}
	
    $parser = new FileParser();
    $parser->load($file);

    $printer = new Printer(new TextFormatter(), $parser);
    $cut = $printer->printAllNote();
    //print_r(serialize($cut));
	$conn = @mysql_connect("localhost","root","") or die("Could not connect: " . mysql_error());
    mysql_select_db("midi",$conn);
	$midinfo = "SELECT * FROM files";
	$result = mysql_query($midinfo,$conn);
	
	$isFound = false;
	$playdir = dirname(dirname(__FILE__)) . '/Midi/Song/';
	
	echo "<h2>";
	while($row = mysql_fetch_array($result,MYSQL_BOTH)) {
		$whole = unserialize($row["content"]);
		if (cmpChannel($cut, $whole)) {
			echo  "Are you searching for ".$row["file_name"]."?";
			$playfile = $playdir . $row["file_name"] . ".mid";
			$isFound = true;
			break;
		}
	}
	if (!$isFound)
		echo "Sorry, we cannot find your song.....";
	else {
	echo "</h2>";
	echo "<div>";
	echo "<!-- WMP -->";
	echo '<OBJECT CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" CODEBASE="http://www.microsoft.com/ntserver/netshow/download/en/nsmp2inf.cab#Version=5,1,51,415" type="application/x-oleobject" width=300 height=44>';
	echo '<PARAM NAME="AutoStart" VALUE="true">';
	echo '<PARAM NAME="FileName" VALUE="'.$playfile.'">';
	echo '<PARAM NAME="ControlType" VALUE="1">';
	echo '<PARAM NAME="Loop" VALUE="false">';
	echo '<PARAM NAME="ShowControls" VALUE="true">';
	echo '<EMBED TYPE="video/x-ms-asf-plugin" PLUGINSPAGE="http://www.microsoft.com/windows/mediaplayer/download/default.asp" SRC="'.$playfile.'" AutoStart="1" ShowControls="1" Loop="0" width=300 height=44>';
	echo '</OBJECT>';
	echo "</div>";
    }
    }
?>





